import 'monaco-editor-core'
import 'monaco-volar'
import * as onigasm from "onigasm";
import onigasmWasm from "onigasm/lib/onigasm.wasm?url";
import { loadGrammars, loadTheme, prepareVirtualFiles } from "monaco-volar";
import editorWorker from "monaco-editor-core/esm/vs/editor/editor.worker?worker";
import vueWorker from "monaco-volar/vue.worker?worker";


// import jsonWorker from "./workers/json.worker.bundle.js?url"
// import cssWorker from "./workers/css.worker.bundle.js?url"
// import htmlWorker from "./workers/html.worker.bundle.js?url"
// import tsWorker from "./workers/ts.worker.bundle.js?url"

function loadMonacoEnv() {
    (self as any).MonacoEnvironment = {
        async getWorker(_: any, label: string) {
            if (label === "vue") {
                return new vueWorker();
            }
            return new editorWorker();
        },
    };
};

function loadOnigasm() {
    return onigasm.loadWASM(onigasmWasm);
}

let isInit = false;

export const onLoader = () => {
    return new Promise((resolve, reject) => {
        if (!isInit) {
            isInit = true;
            Promise.all([loadOnigasm(), loadMonacoEnv()]).then(() => {
                resolve(true);
            });
        } else {
            resolve(true);
        }
    })
}